package com.wangyb.multitenant.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.wangyb.multitenant.domain.MtTenant;
import com.wangyb.multitenant.mapper.MtTenantMapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wangyb.multitenant.bo.MtTenantAddBo;
import com.wangyb.multitenant.bo.MtTenantQueryBo;
import com.wangyb.multitenant.bo.MtTenantEditBo;

import com.wangyb.multitenant.vo.MtTenantVo;
import com.wangyb.multitenant.service.IMtTenantService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 租户Service业务层处理
 *
 * @author ruoyi
 * @date 2021-06-23
 */
@Service
public class MtTenantServiceImpl extends ServiceImpl<MtTenantMapper, MtTenant> implements IMtTenantService {

    @Override
    public MtTenantVo queryById(Long tenantId){
        return getVoById(tenantId, MtTenantVo.class);
    }

    @Override
    public TableDataInfo<MtTenantVo> queryPageList(MtTenantQueryBo bo) {
        PagePlus<MtTenant, MtTenantVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), MtTenantVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<MtTenantVo> queryList(MtTenantQueryBo bo) {
        return listVo(buildQueryWrapper(bo), MtTenantVo.class);
    }

    private LambdaQueryWrapper<MtTenant> buildQueryWrapper(MtTenantQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MtTenant> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getName()), MtTenant::getName, bo.getName());
        lqw.eq(StrUtil.isNotBlank(bo.getSocialCode()), MtTenant::getSocialCode, bo.getSocialCode());
        lqw.eq(StrUtil.isNotBlank(bo.getMobile()), MtTenant::getMobile, bo.getMobile());
        lqw.like(StrUtil.isNotBlank(bo.getChief()), MtTenant::getChief, bo.getChief());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(MtTenantAddBo bo) {
        MtTenant add = BeanUtil.toBean(bo, MtTenant.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(MtTenantEditBo bo) {
        MtTenant update = BeanUtil.toBean(bo, MtTenant.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(MtTenant entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
